﻿$.kish = {}
$(function () {
    $.kish.RegisterUser = {}
    $.extend($.kish.RegisterUser, {
        initializing: function () {
            //member is registering
            var userRoles = new Array();
            userRoles.push(2);
            $('.fn_submit').click(function () { $.kish.RegisterUser.SubmitClick(userRoles) });
            $.kish.RegisterUser.GetCurrentUser();
        },

        //****************************//
        GetCurrentUser: function () {

            $.ajax({
                'type': 'POST',
                'url': '../../Services/UserManagement.svc/GetCurrentUser',
                'data': '',
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    if (result['d'] != null) {
                        $.kish.RegisterUser.SetEditPage(result['d']);
                    }
                },
                'error': function (error) { $.kish.message.alertError("خطا در سیستم"); }
            });
        },

        //****************************//
        SetEditPage: function (user) {
            var FirstName = user.FirstName;
            var LastName = user.LastName;
            var userName = user.Email;
            var Address = user.Address;
            var Telephone = user.Telephone;

            $(".name_fn").val(FirstName);
            $(".lastName_fn").val(LastName);
            $(".fn_userName").val(userName);
            $(".address_fn").val(Address);
            $(".tell_fn").val(Telephone);

        },

        //****************************//
        SubmitClick: function (userRoles) {

            //start--------this part belongs to validation code  "focusout" helps to find empty inputs----------//
            // Kick in Validation
            $('#email').triggerHandler("focusout");
            $('.validaion').each(function () {
                $(this).triggerHandler("focusout");
            });

            if ($('.register mark.error').size() > 0) {
                $('.register').effect('shake', { times: 3 }, 50);
                return false;
            }
            //end--------this part belongs to validation code  "focusout" helps to find empty inputs-----------//


            $('#submit')
            			.before('<img src="../../images/ajax-loader.gif" class="loader" />')


            var FirstName = $.trim($(".name_fn").val());
            var LastName = $.trim($(".lastName_fn").val());
            var Password = $.trim($(".fn_pass").val());
            // var rePass = $.trim($(".fn_rePass").val());
            var userName = $.trim($(".fn_userName").val());
            var Address = $.trim($(".address_fn").val());
            var Telephone = $.trim($(".tell_fn").val());

            var userToInsert = {};
            userToInsert.FirstName = FirstName;
            userToInsert.LastName = LastName;
            userToInsert.Email = userName;
            userToInsert.Telephone = Telephone;
            userToInsert.Password = Password;
            userToInsert.Address = Address;
            userToInsert.Roles = userRoles;
            $.ajax({
                'type': 'POST',
                'url': '../../Services/UserManagement.svc/AddUser',
                'data': JSON.stringify({ user: userToInsert }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {

                    if (result['d'].IsSucceed) {
                        $.kish.message.showUserMessage(result['d'].Message.Message);

                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    };
                    $('#message').slideDown();
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                },
                'error': function (error) { $.kish.message.alertError("خطا در سیستم"); }
            });
        }
    });
    $.kish.RegisterUser.initializing();
});

